package com.wh.concurrent.deadlock;

public class DeadLockTest {
	private String obj;
	
	public DeadLockTest(String obj) {
		this.obj = obj;
	}
	
	public void check1(DeadLockTest o) {
		System.out.println("没有进入代码块之前");
		System.out.println("--------------------");
		synchronized (this) {
			System.out.println("进入对象:" + obj);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			o.check2();
			System.out.println("结束对象的执行:" + obj);
		}
	}
	
	public void check2() {
		synchronized (this) {
			System.out.println("运行了check2方法");
		}
	}
	
	public static void main(String[] args) {
		final DeadLockTest obj1 = new DeadLockTest("obj1");
		final DeadLockTest obj2 = new DeadLockTest("obj2");
		new Thread(){
			@Override
			public void run() {
				obj1.check1(obj2);
			}
			
		}.start();
		new Thread() {
			@Override
			public void run() {
				obj2.check1(obj1);
			}
		}.start();
		System.out.println("主线程结束！");
	}
}
